<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<title></title>
<link rel="stylesheet" type="text/css" href="../static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="../lib/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/style.css" />
</head>
<!--/meta 作为公共模版分离出去-->

<body>
<article class="cl pd-20">
	<form action="" method="post" class="form form-horizontal" id="form-edata-add">
		<input type="hidden" name="id" id="id" value="">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>管理员：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" placeholder="" id="username" name="username" disabled>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">笔名</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" placeholder="" id="pen_name" name="pen_name">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">手机：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" placeholder="" id="phone" name="phone">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">邮箱：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" placeholder="@" name="email" id="email">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">角色：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<span class="select-box" style="width:150px;">
					<select class="select" name="role_id" id="role" size="1">
					</select>
				</span>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3">备注：</label>
			<div class="formControls col-xs-4 col-sm-6">
				<textarea name="note" id="note" cols="" rows="" class="textarea"  placeholder="说点什么...100个字符以内" dragonfly="true" onKeyUp="textarealength(this,100)"></textarea>
				<p class="textarea-numberbar"><em class="textarea-length">0</em>/100</p>
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<input class="btn btn-primary radius" type="button" id="button" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			</div>
		</div>
	</form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="../lib/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="../lib/layer/2.4/layer.js"></script> 
<script type="text/javascript" src="../static/h-ui/js/H-ui.js"></script> 
<script type="text/javascript" src="../static/h-ui.admin/js/H-ui.admin.page.js"></script> 
<!--/_footer /作为公共模版分离出去-->

<script  type="text/javascript" src="../static/js/admin.js"></script>

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/jquery.validate.js"></script> 
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/messages_zh.js"></script> 
<script type="text/javascript">	
	var id = 0;
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");

		if (pair[0] == "id") {
			id = parseInt(pair[1]);
        }
    }
	// console.log(id);
	$("#id").val(id)
	$.ajax({
		type:"POST",
		url:"/ajax/adminuser/getuser",
		data:{"id":id},
		dataType:"json",
		beforeSend:function(request){
			request.setRequestHeader("authStr", window.localStorage.getItem("userId")+"-"+window.localStorage.getItem("token"));
		},
		success:function(data){
			if (data.success==true){
				var userData =  data.data;
				console.log(userData);
				$("#username").val(userData.username);
				$("#pen_name").val(userData.pen_name);
				$("#phone").val(userData.phone);
				$("#email").val(userData.email);
				$("#note").val(userData.note);
				var adminRoles = userData.roleList;
				var sel_str = "";
				adminRoles.forEach(role => {
					if (userData.role_id==role.id) {
						sel_str += '<option value="'+role.id+'" selected>'+role.name+'</option>';
					} else {
						sel_str += '<option value="'+role.id+'">'+role.name+'</option>';
					}
				});
				$("#role").append(sel_str);
			}
		}
	});

	// 提交表单
	$(function(){
		$("#button").on('click', function(){
			if(validform().form()) {
				//通过表单验证，以下编写自己的代码
				var edata = $("#form-edata-add").serialize();
				$.ajax({
					type: 'post',
					url: '/ajax/adminuser/edit',
					data: edata,
					dataType: "json",
					beforeSend:function(request){
						request.setRequestHeader("authStr", window.localStorage.getItem("userId")+"-"+window.localStorage.getItem("token"));
					},
					success: function(data) {
						// console.log(data);
						if (data.success==true) {
							layer.msg(data.msg, {icon:6,time:1000}, function(){
								window.parent.location.reload();
								var index = parent.layer.getFrameIndex(window.name);
								parent.layer.close(index);
							});
						} else {
							layer.msg(data.msg, {icon:5,time:2000});
						}
						return false;
					}
				});
				return false;
			} else {
				//校验不通过，什么都不用做，校验信息已经正常显示在表单上
				layer.msg("请按要求填写表单", {icon:5, time:2000});
				return false;
			}
		});
	});

	//编写表单验证函数validform，在验证按钮注册按钮点击事件内调用验证函数对象
	function validform(){
	/*关键在此增加了一个return，返回的是一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/
		return $("#form-edata-add").validate({
			rules : {
				tableNameEN : {
					required : true
				},
			},
			messages : {
				tableNameEN : {
					required : '表名必填',
				},
			},
			onkeyup:false,
			focusCleanup:true,
			success:"valid"
		});
	}
	//注册表单验证
	$(validform());
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>